﻿@model SimpleForum.Data.Models.Thread

@{
    ViewBag.Title = Model.Name;
}

<div id="content-top">
    <h2>@ViewBag.Title</h2>
    <span class="align-right">
        @Html.ActionLink("post a reply", "reply", "thread", new { id = Model.Id }, null)
    </span>
    <span class="clearfix">&nbsp;</span>
</div>

<div class="box" id="@Model.Id">
    <div class="box-container">
        <div class="align-right" id="post-stats" style="width:200px;">
            @if (Model.Tags.Any()) {
            <div class="tags">tags<br />
                @Html.DisplayFor(x => x.Tags)
            </div>
            }
            
            <div class="post-info">created <strong>@Html.DisplayFor(x => x.CreatedDate)</strong></div>
            <div class="post-info">views <strong>@Model.Views</strong></div>
            <div class="post-info">last post <strong>@Html.DisplayFor(x => x.Posts.LastOrDefault().CreatedDate)</strong></div>
        </div>

        @Html.DisplayFor(x => x.Posts)

        <span class="clearfix">&nbsp;</span>
    </div>
</div>

@Html.Resource(
    @<script type="text/javascript" language="javascript">
         $(document).ready(function () {
             @if(User.Identity.IsAuthenticated) { <text>
             $('.post-action.post-rating').click(function () {
                 var rating = $('img', this).attr('alt');
                 var postid = $(this).parents('.post').attr('id').substring(5);
                 var parent = $(this).parent();

                 $(parent).html('<em>loading...</em>');
                 $.post('/Post/Rate', { id: postid, rating: rating }, function (d) {
                     if(d == 'true') $(parent).html('<em>reload the page to see ratings</em>');
                     else $(parent).html('<em>error</em>');
                 });

                 return false;
             });

             $('.report-action a').click(function () {
                 var reportreason = $(this).attr('class');
                 var postid = $(this).parents('.post').attr('id').substring(5);
                 var parent = $(this).parent();
                 var reason = $('textarea', parent).val();

                 $(parent).html('<em>loading...</em>');
                 $.post('/Post/Report', { id: postid, reportreason: reportreason, reason: reason }, function (d) {
                     if(d == 'true') $(parent).html('<em>this post has been reported</em>');
                     else $(parent).html('<em>error</em>');
                 });

                 return false;
             });
             </text> }
         });
    </script>
, "js")